/**
 ******************************* ICE *******************************
 * ========================================================================
 * Project:		icemasterdataservices
 * Package:		org.ice4apps.master.dataservices
 * File:		IceMasterDataServices.java
 * Author: 		robin
 * Date: 		Mar 27, 2009
 * Description:	
 * ========================================================================
 * 
 */
package org.ice4apps.master.dataservices;

import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.ice4apps.master.dao.Client;
import org.ice4apps.master.dao.Enterprise;
import org.ice4apps.master.dao.Office;
import org.ice4apps.master.dao.Organization;
import org.ice4apps.master.dao.Session;
import org.ice4apps.master.dao.SystemAdmin;
import org.ice4apps.master.dao.User;

@Stateless
public class IceMasterDataService implements
                                  IceMasterDataServiceLocal {
    @PersistenceContext(unitName = "icemasterdaoPU")
    private EntityManager entityManager;

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServicesLocal#addSession(org
     * .ice.master.dao.Session)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void addSession (Session session) {
        entityManager.persist (session);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServicesLocal#addSystemAdmin
     * (org.ice.master.dao.SystemAdmin)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void addSystemAdmin (SystemAdmin admin) {
        entityManager.persist (admin);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServicesLocal#cleanupSessions
     * (long)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void cleanupSessions (long interval) {
        final long cleanupTime = System.currentTimeMillis () - interval;
        final String query = "delete Session session where session.loginRefreshTime < :cleanuptime";
        entityManager.createQuery (query).setParameter ("cleanuptime",
                                                        cleanupTime)
                .executeUpdate ();
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServicesLocal#getSession(java
     * .lang.String)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Session getSession (String id) {
        return entityManager.find (Session.class, id);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServicesLocal#getSystemAdmin
     * (java.lang.String)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public SystemAdmin getSystemAdmin (String id) {
        return entityManager.find (SystemAdmin.class, id);
    }

    /*
     * (non-Javadoc)
     * @seeorg.ice.master.dataservices.IceMasterDataServicesLocal#
     * getSystemAdminByUsername(java.lang.String)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public SystemAdmin getSystemAdminByUsername (String username) {
        final String query = "select admin from SystemAdmin admin where admin.username=:username";
        final Object admin = entityManager.createQuery (query)
                .setParameter ("username", username).getSingleResult ();
        if (admin != null)
            return (SystemAdmin) admin;
        return null;
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServicesLocal#modifySystemAdmin
     * (org.ice.master.dao.SystemAdmin)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void modifySystemAdmin (SystemAdmin admin) {
        entityManager.merge (admin);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServicesLocal#refreshSession
     * (java.lang.String)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void refreshSession (String id) {
        final String query = "update Session session set session.loginRefreshTime=:time where session.id=:id";
        entityManager.createQuery (query)
                .setParameter ("time", System.currentTimeMillis ())
                .setParameter ("id", id).executeUpdate ();
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServicesLocal#removeSession(
     * java.lang.String)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void removeSession (String id) {
        final String query = "delete Session session where session.id=:id";
        entityManager.createQuery (query).setParameter ("id", id)
                .executeUpdate ();
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServicesLocal#removeSystemAdmin
     * (java.lang.String)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void removeSystemAdmin (String id) {
        final String query = "delete SystemAdmin admin where admin.id=:id";
        entityManager.createQuery (query).setParameter ("id", id)
                .executeUpdate ();
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServicesLocal#
     * removeSystemAdminByUsername(java.lang.String)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void removeSystemAdminByUsername (String username) {
        final String query = "delete SystemAdmin admin where admin.username=:username";
        entityManager.createQuery (query).setParameter ("username", username)
                .executeUpdate ();
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#addEnterprise(org.ice4apps.master.dao.Enterprise)
     */
    public void addEnterprise (Enterprise enterprise) {
        entityManager.persist (enterprise);
        
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#deleteEnterprise(java.lang.String)
     */
    public void deleteEnterprise (String id) {
        // TODO Auto-generated method stub
        
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#getClientByKey(java.lang.String)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Client getClientByKey (String key) {
        final String query = "select client from Client client where client.clientKey=:key";
        final Object client = entityManager.createQuery (query)
                .setParameter ("key", key).getSingleResult ();
        if (client != null)
            return (Client) client;
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#modifyEnterprise(org.ice4apps.master.dao.Enterprise)
     */
    public void modifyEnterprise (Enterprise enterprise) {
        entityManager.merge (enterprise);
        
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServiceLocal#getEnterprise(java
     * .lang.String)
     */
    public Enterprise getEnterprise (String id) {
        return entityManager.find (Enterprise.class, id);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServiceLocal#getEnterpriseByName
     * (java.lang.String)
     */
    public Enterprise getEnterpriseByName (String name) {
        final String query = "select ent from Enterprise ent where ent.name=:name";
        final Object enterprise = entityManager.createQuery (query)
                .setParameter ("name", name).getSingleResult ();
        if (enterprise != null)
            return (Enterprise) enterprise;
        return null;
    }

    /*
     * (non-Javadoc)
     * @see
     * org.ice.master.dataservices.IceMasterDataServiceLocal#getEnterpriseList()
     */
    public List<Enterprise> getEnterpriseList () {
        final String query = "select ent from Enterprise ent";
        return entityManager.createQuery (query).getResultList ();
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#addOrganization(org.ice4apps.master.dao.Organization)
     */
    public void addOrganization (Organization org) {
        entityManager.persist (org);
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#getOrganizationByName(java.lang.String)
     */
    public Organization getOrganizationByName (String name) {
        final String query = "select org from Organization org join fetch org.enterprise where org.name=:name";
        final Object org = entityManager.createQuery (query)
                .setParameter ("name", name).getSingleResult ();
        if (org != null)
            return (Organization) org;
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#getOrganizationListByEnterprise(java.lang.String)
     */
    public List<Organization> getOrganizationListByEnterprise (String enterpriseName) {
        final String query = "select org from Organization org join fetch org.enterprise where org.enterprise.name=:entName";
        return entityManager.createQuery (query).setParameter ("entName", enterpriseName).getResultList ();
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#addOffice(org.ice4apps.master.dao.Office)
     */
    public void addOffice (Office office) {
        entityManager.persist (office);
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#getOfficeByName(java.lang.String)
     */
    public Office getOfficeByName (String name) {
        final String query = "select office from Office office join fetch office.organization join fetch office.organization.enterprise where office.name=:name";
        final Object office = entityManager.createQuery (query)
                .setParameter ("name", name).getSingleResult ();
        if (office != null)
            return (Office) office;
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#getOfficeListByOrganization(java.lang.String)
     */
    public List<Office> getOfficeListByOrganization (String orgName) {
        final String query = "select office from Office office join fetch office.organization join fetch office.organization.enterprise where office.organization.name=:orgName";
        return entityManager.createQuery (query).setParameter ("orgName", orgName).getResultList ();
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#addUser(org.ice4apps.master.dao.User)
     */
    public void addUser (User user) {
        entityManager.persist (user);
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#getUserByUserName(java.lang.String)
     */
    public User getUserByUserName (String userName) {
        final String query = "select user from User user join fetch user.office join fetch user.office.organization join fetch user.office.organization.enterprise where user.username=:username";
        final Object user = entityManager.createQuery (query)
                .setParameter ("username", userName).getSingleResult ();
        if (user != null)
            return (User) user;
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice.master.dataservices.IceMasterDataServiceLocal#getUserListByOffice(java.lang.String)
     */
    public List<User> getUserListByOffice (String officeName) {
        final String query = "select user from User user join fetch user.office join fetch user.office.organization join fetch user.office.organization.enterprise where user.office.name=:officeName";
        return entityManager.createQuery (query).setParameter ("officeName", officeName).getResultList ();
    }
}
